<!--
 * @Descripttion: 轮播图设置
 * @Author: xiao li
 * @Date: 2020-12-07 10:50:31
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 14:20:33
-->
<template>
    <div class="lb-goods-classify">
        <top-nav :isBack="true" />
        <div class="page-main">
          <el-row>
            <lb-button type='primary' icon="el-icon-plus" @click="$router.push(`/website/offer/editBanner?modular_id=${searchForm.modular_id}`)">{{$t('menu.PayclassBannerAdd')}}</lb-button>
          </el-row>
          <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            tooltip-effect="dark"
            style="width: 100%"
           >
            <el-table-column
              prop="id"
              label="ID">
            </el-table-column>
            <el-table-column
              prop="cover"
              label="图片">
                <template slot-scope="scope">
                  <lb-image :src="scope.row.cover" />
                </template>
            </el-table-column>
            <el-table-column
              prop="cate_name"
              label="所属分类">
            </el-table-column>
            <el-table-column
              prop="top"
              label="排序值">
            </el-table-column>
            <el-table-column
              prop="status"
              label="是否上架">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="updateItem(scope.row.id,scope.row.status)"
                    >
                  </el-switch>
                </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间">
              <template slot-scope="scope">
                <div>{{scope.row.create_time | handleTime(1)}}</div>
                <div>{{scope.row.create_time | handleTime(2)}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <lb-button size='mini' plain type='primary' @click="$router.push(`/website/offer/editBanner?modular_id=${searchForm.modular_id}&id=${scope.row.id}`)">{{$t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="confirmDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      total: 0,
      searchForm: {
        page: 1,
        limit: 10,
        modular_id: 0
      },
      tableData: [],
      multipleSelection: [],
      showTable: true
    }
  },
  activated () {
    let { modular_id: id } = this.$route.query
    this.searchForm.modular_id = id
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 获取分类列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.websiteStarBannerList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method 删除表格分类值
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    },
    /**
     * @method 上下架/删除分类
     */
    async updateItem (id, status) {
      this.$api.websiteStarBannerUpdate({id, status}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
  .lb-goods-classify{
    width: 100%;
    .page-main{
      padding: 20px;
      .el-row{
        margin-bottom: 20px;
        .el-button{
          margin-right: 24px;
          &:last-child{
            margin-right: 0;
          }
        }
      }
    }
  }
</style>
